#coding=utf-8
from uliweb import expose

#可以省略直接引用
from uliweb import request, error, redirect
from sqlalchemy import desc,and_

def __begin__():
    """
    用户验证
    """
    from uliweb import functions
    return functions.require_login()

@expose('/webprojectpost')
class PostData(object):
    

    def __init__(self):
        from uliweb.orm import get_model
        self.subversionmodel = get_model('subversion')
        self.usermodel = get_model('user')
    
    def subversionlist(self):
        """
        
        返回所有仓库的列表信息
        """

        #得到偏移量
        pageIndex= request.GET.get('offset',0)
        #得到查询个数
        pageSize = request.GET.get('limit',20)
    
        #得到搜索字段
        search_key = request.GET.get('search')
    
        #得到排序字段
        sort = request.GET.get('sort','id')
        #得到排序规则
        order_by_type = request.GET.get('order','asc')
    
        #使用sqlchemy自带asc方法，uliweb版本  self.usermodel.c['id'].asc()或self.usermodel.c.id.asc()
        if order_by_type == 'asc':
            #升序
            #order_by = asc('id')
            order_by = self.subversionmodel.c[sort].asc()
        else:
            #降序
            #order_by = desc ('id')
            order_by = self.subversionmodel.c[sort].desc()
    
        data = {}
        data['rows'] = []


        data['total'] = self.subversionmodel.all().count()
        ormdata = self.subversionmodel.all().order_by(order_by).limit(pageSize).offset(pageIndex)
        
        user = request.user
                     
        u = []
        for i in ormdata:               
            
            user_list = i.users.ids()
            
            for j in i.users:
                u.append(j.nickname)
                
            if user.is_superuser:
                
                data['rows'].append({"id":i.id,
                                     "svn_name":i.svn_name,
                                     "svn_username":i.svn_username,
                                     "svn_passwd":i.svn_passwd,
                                     "svn_url":i.svn_url,
                                     "svn_webdir_server":i.svn_webdir_server,
                                     "rsync_ignore_dir":i.rsync_ignore_dir,
                                     "rsync_module":i.rsync_module,
                                     "svn_comment":i.svn_comment,
                                     "svn_webip_client":i.svn_webip_client,
                                     "username":",".join(u)
                                     })
            else:
                if user in user_list:
                    data['rows'].append({"id":i.id,
                                         "svn_name":i.svn_name,
                                         "svn_username":i.svn_username,
                                         "svn_passwd":i.svn_passwd,
                                         "svn_url":i.svn_url,
                                         "svn_webdir_server":i.svn_webdir_server,
                                         "rsync_ignore_dir":i.rsync_ignore_dir,
                                         "rsync_module":i.rsync_module,
                                         "svn_comment":i.svn_comment,
                                         "svn_webip_client":i.svn_webip_client,
                                         "username":",".join(u)
                                         })                    
         
        #返回json串
        return json(data)
